package main

import (
	"fmt"
	"github.com/streadway/amqp"
	"log"
	// "time"
)

func failOnError(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %s", msg, err)
	}
}

func main() {
	exchange := "xtrans_ds_exchange"
	exchangeType := "fanout"

	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	err = ch.ExchangeDeclare(
		exchange,     // name
		exchangeType, // type
		true,         // durable
		false,        // auto-deleted
		false,        // internal
		false,        // noWait
		nil,          // arguments
	)
	failOnError(err, "Exchange Declare")

	queue, err := ch.QueueDeclare(
		"hello", // name of the queue
		true,    // durable
		false,   // delete when unused
		false,   // exclusive
		false,   // noWait
		nil,     // arguments
	)
	failOnError(err, "Queue Declare")

	fmt.Printf("queue name %s", queue.Name)

	err = ch.QueueBind(
		queue.Name, // name of the queue
		"",         // bindingKey
		exchange,   // sourceExchange
		false,      // noWait
		nil,        // arguments
	)
	failOnError(err, "QueueBind")

	msgs, err := ch.Consume(
		"",    // name
		"",    // consumerTag,
		false, // noAck
		false, // exclusive
		false, // noLocal
		false, // noWait
		nil,   // arguments
	)
	failOnError(err, "Failed to consume a message")
	// fmt.Printf("%+v\n", string(msg))
	// time.Sleep(1 * time.Duration(time.Second))
	for d := range msgs {
		log.Printf(
			"got %dB delivery: [%v] %q",
			len(d.Body),
			d.DeliveryTag,
			d.Body,
		)
		d.Ack(false)
	}
}
